<script lang="ts" setup>
import { hBtns, UiDataView ,openModal} from '../../utils'
import { onMounted, ref } from 'vue'
import { DataViewRef, PageFetchAction, TableCellBtn, TableColumnConfig } from '../../utils/types'

const tableRef: DataViewRef = ref()

const columns = [
  { label: '卖家名称', prop: 'ruleName' },
  { label: '卖家信用代码' },
  { label: '创建时间时间' },
  { label: '状态' },
  {
    label: '操作',
    fixed: 'right',
    width: 160,
    render (h, { row }) {
      return hBtns([
        { label: '编辑', onClick: onEdit.bind(null, row) },
        { label: '账户', onClick: onCheck.bind(null, row) }
      
      ] as TableCellBtn[])
    }
  }
] as TableColumnConfig<any>[]

const fetchAction: PageFetchAction = params => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        total: 2,
        rows: [{ ruleName: 'test' }, { ip: '168.12.3.4' }]
      })
    }, 500)
  })
}

const onDelete = (rows: any[]) => {
  tableRef.value.deleteRows(() => {
    return Promise.resolve()
  })
}
const onEdit = (row?: any) => {
  openModal(() => import('./CustomEdit.vue'), {
    row,
    title: `${row ? '编辑'+ row.ruleName +'的账户' : '新增'}`,
    onSubmitted ({ form }) {
    }
  })
}

const onCheck = (row?: any) => {
  openModal(() => import('./CheckAccount.vue'), {
    row,
    width: '800px',
    title: '查看'+row.ruleName+'的账户'
  })
}
onMounted(async () => {
  await tableRef.value.setInitForm({ namelist: '1' })
  tableRef.value.getData()
})
</script>

<template>
  <UiDataView ref="tableRef" :columns="columns" :fetch-action="fetchAction">
    <template #search="{ form, onSearch }">
      <ElFormItem label="API名称">
        <ElInput v-model="form.ruleName" @keyup.enter="onSearch" placeholder="请输入" />
      </ElFormItem>
      <ElFormItem label="创建时间">
        <ElDatePicker
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          range-separator="至"
          v-model="form.timerange" />
      </ElFormItem>
      <ElFormItem label="卖家信用代码">
        <ElInput placeholder="请输入" />
      </ElFormItem>
      <ElFormItem label="状态">
        <ElSelect clearable></ElSelect>
      </ElFormItem>
    </template>
  </UiDataView>
</template>
